package com.itheima.stock.security.service;

import com.itheima.stock.mapper.SysPermissionMapper;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.entity.SysPermission;
import com.itheima.stock.pojo.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 定义用户详情服务bean
 */

@Service("userDetailService")
public class LoginUserDetailService implements UserDetailsService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysPermissionMapper permissionMapper;

    /**
     * 根据传入的用户名称获取胡相关的详情信息 用户名 密文密码 权限信息等
     * @param username the username identifying the user whose data is required.
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//1.根据用户名称获取数据库中用户的核心信息
        if (StringUtils.isBlank(username)) {
            throw new UsernameNotFoundException("用户名输入错误");
        }
        SysUser sysUser = sysUserMapper.findUserInfoByUserName(username);
        if (sysUser==null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //2.组装UserDetails认证对象
        //组装权限集合
        //2.1思考：需要哪些宇段？用户名密文密码权限集合（web接口绑定表示的集合）等
        //2.2 其它字段：id email phone menus permissions 等
         //取指定用户的权限集合添加获取侧边栏数据和按钮权限的结合信息
        SysPermission sysPermission = permissionMapper.selectByPrimaryKey(sysUser.getId());
        return null;
    }
}
